using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
using System.Text;
using System.Threading;
namespace HKB.Common
{
	public class ZipFile
	{
		public void Create(string[] files, string baseDir, System.IO.Stream destinationStream, int level, string FileStr, string FileName)
		{
			ZipOutputStream zipOutputStream = new ZipOutputStream(destinationStream);
			zipOutputStream.SetLevel(level);
			if (!baseDir.EndsWith("\\"))
			{
				baseDir += "\\";
			}
			Crc32 crc = new Crc32();
			string text = "";
			int num = 0;
			ZipEntry zipEntry = null;
			System.IO.FileStream fileStream = null;
			for (int i = 0; i < files.Length; i++)
			{
				fileStream = null;
				files[i] = files[i].Substring(baseDir.Length);
				zipEntry = new ZipEntry(files[i]);
				for (int j = 0; j < 5; j++)
				{
					try
					{
						fileStream = new System.IO.FileStream(baseDir + files[i], System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
						break;
					}
					catch
					{
						fileStream = null;
						System.Threading.Thread.Sleep(1000);
					}
				}
				if (fileStream == null)
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"打开文件失败: ",
						baseDir,
						files[i],
						"\r\n"
					});
					num++;
				}
				else
				{
					byte[] array = new byte[65536];
					crc.Reset();
					zipOutputStream.PutNextEntry(zipEntry);
					int num2;
					do
					{
						num2 = fileStream.Read(array, 0, array.Length);
						zipOutputStream.Write(array, 0, num2);
						crc.Update(array, 0, num2);
					}
					while (num2 > 0);
					fileStream.Close();
					zipEntry.Crc = crc.Value;
					System.IO.FileInfo fileInfo = new System.IO.FileInfo(baseDir + files[i]);
					zipEntry.DateTime = fileInfo.LastWriteTime;
					zipEntry.Size = fileInfo.Length;
				}
			}
			if (num == 0)
			{
				text = "压缩成功 [" + System.DateTime.Now.ToString("R") + "]";
			}
			else
			{
				string text2 = text;
				text = string.Concat(new string[]
				{
					text2,
					"压缩过程中出现 ",
					num.ToString(),
					" 错误 [",
					System.DateTime.Now.ToString("R"),
					"]"
				});
			}
			byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);
			zipEntry = new ZipEntry("ZipReport.txt");
			crc.Reset();
			zipOutputStream.PutNextEntry(zipEntry);
			zipOutputStream.Write(bytes, 0, bytes.Length);
			crc.Update(bytes);
			zipEntry.Crc = crc.Value;
			zipEntry.DateTime = System.DateTime.Now;
			zipEntry.Size = (long)bytes.Length;
			if (FileStr != null && FileName != null)
			{
				byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(FileStr);
				zipEntry = new ZipEntry(FileName);
				crc.Reset();
				zipOutputStream.PutNextEntry(zipEntry);
				zipOutputStream.Write(bytes2, 0, bytes2.Length);
				crc.Update(bytes2);
				zipEntry.Crc = crc.Value;
				zipEntry.DateTime = System.DateTime.Now;
				zipEntry.Size = (long)bytes2.Length;
			}
			zipOutputStream.Finish();
			zipOutputStream.Close();
		}
		public void Create(string[] files, string baseDir, string destination, int level, string FileStr, string FileName)
		{
			this.Create(files, baseDir, System.IO.File.Create(destination), level, FileStr, FileName);
		}
	}
}
